package com.sq.partner.manager.controller;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.TradConfig;
import com.sq.partner.manager.service.CarAccidentService;
import com.sq.partner.manager.service.IGatewayLifePolicyListService;
import com.sq.partner.manager.util.JsonUtils;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import fr.opensagres.xdocreport.document.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2022/9/9 20:18
 */
@Controller
@RequestMapping("/CarAccidentController")
public class CarAccidentController {
	private static Logger logger = LogUtil.getLog(GatewayUserController.class);
	@Autowired
	private CarAccidentService carAccidentService;

	@Autowired
	IGatewayLifePolicyListService lifePolicyListService;

	@RequestMapping("/index")
	public String CarAccidentIndex(){
		return "nlifeProduct/carAccident/carAccidentPlanOrder";
	}

	/**
	 * 查询当前用户的权限机构
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryPrivilegeBranch",method = RequestMethod.POST)
	@ResponseBody
	public Result queryPrivilegeBranch(HttpSession session) throws Exception {
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		logger.info("当前登录用户的userCode为"+user.getUserCode()+",DepartmentStr:"+user.getDepartmentStr());

		if(null != user && StrUtil.isNotEmpty(user.getDepartmentStr())){
			return Result.data(carAccidentService.queryPrivilegeBranch(user.getDepartmentStr()));
		}

		return Result.fail();
	}
	/**
	 * 查询人险相关合作伙伴
	 * @param fkAppid
	 * @return List<GatewayChannelInfo>
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryChannelInfoOfLife",method = RequestMethod.POST)
	public @ResponseBody Page<GatewayChannelInfo> queryChannelInfoOfLife(Integer size,Integer pageIndex,
																		 String parentDeptCode,String channelName,String channelCode) throws Exception{
		if(StrUtil.isEmpty(parentDeptCode)){
			throw new AppException("机构不能为空");
		}

		Page<GatewayChannelInfo> page = new Page<GatewayChannelInfo>();
		page.setCurrent(pageIndex);
		page.setSize(size);

		GatewayChannelInfo channelInfo = new GatewayChannelInfo();
		channelInfo.setDeptCode(parentDeptCode);
		channelInfo.setChannelCode(channelCode);
		channelInfo.setChannelName(channelName);
		page.setType(channelInfo);
		return lifePolicyListService.queryChannelInfoPageOfLife(page);
	}

	/**
	 * 查询conf方案
	 * @param pageIndex
	 * @param privilegeBranch
	 * @param partnerNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryConfigPlanByCriteria",method = RequestMethod.POST)
	@ResponseBody
	public Page queryConfigPlanByCriteria(Integer pageIndex,String privilegeBranch,String partnerNo) throws Exception {
		Page<Map<String, Object>> page = new Page<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String,Object>();
		if(pageIndex != -1){
			page.setCurrent(pageIndex);
			map = SqlUtil.buildPageMap(page);
		}
		map.put("privilegeBranch",privilegeBranch.substring(0,2));
		map.put("partnerNo",partnerNo);
		page.setMap(map);
		page = carAccidentService.queryConfigPlanByCriteria(page);
		return page;
	}

	@RequestMapping(value = "/updateConfigPlanList",method = RequestMethod.POST)
	@ResponseBody
	public Result updateConfigPlanList(@RequestBody List<TradConfig> configArrayList) throws Exception {
		Result result = carAccidentService.updateConfigPlanList(configArrayList);
		return result;
	}
}
